package com.dys.processcontrol.circularstructure;

import java.util.Scanner;

/**
 * @className: com.dys.processcontrol.circularstructure.DoWhileTest
 * @description: do while循环
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/06/19 下午8:31
 */
public class DoWhileTest {

    public static void main(String[] args) {
        doWhileTest();
    }

    /**
     * do while方式：
     * 1+2+3+4+5+...100
     */
    public static void sum() {
        int num = 1;
        int sum = 0;
        do {
            sum += num;
            num++;
        } while (num <= 100);// 要加分号，否则编译会出错

        System.out.println("sum=" + sum);
    }

    /**
     * 与while循环的区别：
     * while: 先判断，后执行
     * do while: 先执行，后判断-->至少被执行一次，从第二次才进行判断。
     * do while应用场景：
     * while(考试是否通过){
     * 考试;
     * } ==>不合适
     * ------------
     * do {
     * 考试;
     * }while(判断考试是否通过);==>合适
     */
    public static void sum2() {
        int num = 101;
        int sum = 0;

        while (num <= 100) {
            sum += num;
            num++;
        }
        System.out.println(num);
        System.out.println("while循环得到的结果：sum=" + sum);
        System.out.println("----------------------------");
        // do while循环
        num = 101;
        sum = 0;
        do {
            sum += num;
            num++;
        } while (num <= 100);// 要加分号，否则编译会出错
        System.out.println(num);
        System.out.println("do while得到的结果: sum=" + sum);
    }

    /**
     * 要求用户输入一个数字，直到用户输入的数字大于10为止：
     */
    public static void doWhileTest() {
        Scanner sc = new Scanner(System.in);
        int num;
        do {
            System.out.print("请输入一个数字,直到输入的数字大于10方可结束: ");
            num = sc.nextInt();
        } while (num <= 10);
        System.out.println("输入的数字大于10，程序结束。");
        sc.close();
    }
}
